<template>
  <div class="app-container">
    <!-- 筛选、新增 -->
    <div class="search-container">
      <!-- <el-form :inline="true">
        <el-form-item>
          <el-input
            prefix-icon="el-icon-search"
            type="text"
            style="width: 500px"
            placeholder="请输入关键词..."
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="search-button">搜索</el-button>
        </el-form-item>
      </el-form> -->
      <el-form>
        <el-form-item v-if="checkPermission(['校内老师', '企业导师'])">
          <router-link to="/project/addProject">
            <el-button type="primary">新增项目</el-button>
          </router-link>
        </el-form-item>
      </el-form>
    </div>
    <!-- 项目列表 -->
    <div>
      <el-table
        :data="list"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="序号" min-width="30%" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="名称" min-width="50%" align="center">
          <template slot-scope="scope">
            <el-link type="info" :underline="false" @click="toDetail(scope.row.projid)">
              {{ scope.row.name }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" min-width="60%" align="center">
          <template slot-scope="scope">
            {{ scope.row.reltime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="60%" align="center">
          <template slot-scope="scope">
            <el-button v-if="checkPermission(['校内老师', '企业导师'])" type="success" size="mini" @click="handleEditProInfo(scope.row.projid)">编辑信息</el-button>
            <el-button v-if="checkPermission(['校内老师', '企业导师'])" type="primary" size="mini" @click="handleParticipants(scope.row.projid)">成员管理</el-button>
            <el-button v-if="checkPermission(['校内老师', '企业导师'])" type="primary" size="mini" @click="ModifyComment(scope.row)">修改评语</el-button>
            <el-button v-if="checkPermission(['校内老师', '企业导师'])" type="primary" size="mini" @click="handleDeletePro(scope.row.projid)">删除项目</el-button>
            <el-button v-if="checkPermission(['学生'])" type="primary" size="mini" @click="handleAddTrends(scope.row.projid)">添加项目动态</el-button>
            <el-button v-if="checkPermission(['学生'])" type="primary" size="mini" @click="handleUpdateRole(scope.row.projid)">修改个人信息</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pagenum" :limit.sync="listQuery.pagesize" @pagination="fetchData" />
    </div>

    <!-- 学生修改个人信息对话框 -->
    <el-dialog title="修改个人信息" :visible.sync="dialogUpdateForm">
      <el-form
        ref="roleInfo"
        :model="roleInfo"
        :rules="roleRules"
      >
        <el-form-item label="我在项目中的角色:" label-width="150px" prop="role">
          <el-input ref="role" v-model="roleInfo.role" type="textarea" placeholder="在项目中担任的角色，不超过100字" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="handleSubmitUpdateRole">提交</el-button>
        <el-button>取消</el-button>
      </div>
    </el-dialog>

    <!-- 添加项目动态对话框 -->
    <el-dialog title="添加项目动态" :visible.sync="dialogAddTrends">
      <el-form
        ref="proTrends"
        :model="proTrends"
        :rules="trendRule"
      >
        <el-form-item label="正文:" prop="maintext">
          <div>
            <span>请将必要的辅助材料压缩上传附件</span>
          </div>
          <Tinymce ref="editor" v-model="proTrends.maintext" />
        </el-form-item>
        <el-form-item label="附件:">
          <el-upload
            ref="upload"
            :action="'/api/sys/addProgressAttach'"
            :data="proTrends"
            :limit="limit"
            :auto-upload="false"
            :on-success="successSubmit"
            accept=".xls, .xlsx, .zip, .pdf, .doc, .png, .jpeg, .jpg, .txt"
            :headers="headers"
          >
            <el-button slot="trigger" type="info" size="small">上传附件</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" style="width: 100px" @click="handleAddProgress">提交</el-button>
      </div>
    </el-dialog>
    <!-- 修改评语对话框 -->
    <el-dialog title="修改评语" :visible.sync="dialogCommentVisible">
      <el-form label-width="70px" style="width: 60%; margin-left: 50px">
        <el-form-item label="评分：">
          <el-input v-model="projectInfo.score" />
        </el-form-item>
        <el-form-item label="评语:" prop="comment">
          <el-input v-model="projectInfo.comment" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" style="width: 100px" @click.native="handleModify">
          提交
        </el-button>
        <el-button @click="dialogCommentVisible = false">
          取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import Pagination from '@/components/Pagination'
import { getProList, updateProRole, addProgress, updateCommentInfo, deletePro } from '@/api/project'
import Tinymce from '@/components/Tinymce'
import { getToken } from '@/utils/auth'
export default {
  name: 'ProList',
  components: { Pagination, Tinymce },
  data() {
    const validateRole = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户角色'))
      }
      if (value.length > 100) {
        callback(new Error('最多输入100字'))
      }
      callback()
    }
    return {
      listQuery: {
        pagenum: 0,
        pagesize: 10
      },
      total: 0,
      limit: 1,
      headers: {
        Authentication: getToken()
      },
      list: null,
      // 角色信息
      roleInfo: {
        role: '',
        projid: 0
      },
      roleRules: {
        role: [{ required: true, trigger: 'blur', validator: validateRole }]
      },
      // 项目动态
      proTrends: {
        projid: 0,
        maintext: ''
      },
      trendRule: {
        maintext: [{ required: true, trigger: 'blur', message: '正文内容不可为空' }]
      },
      dialogUpdateForm: false, // 学生修改个人信息对话框
      dialogAddTrends: false,
      dialogCommentVisible: false, // 修改评语对话框
      projectInfo: {
        projid: '',
        name: '',
        maintext: '',
        ispublic: 0,
        reltime: '',
        score: 0,
        comment: '',
        attachment: ' '
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    checkPermission,
    // 获取项目列表
    fetchData() {
      getProList(this.listQuery).then(res => {
        this.list = res.data.list
        this.total = res.data.total
      }).catch(() => {
        this.$message.error('获取项目列表错误')
      })
    },
    // 编辑项目信息
    handleEditProInfo(id) {
      // this.$router.push('/project/edit/' + id)
      this.$router.push({ name: 'ProEdit', params: { id: id }})
    },
    // 项目成员管理
    handleParticipants(id) {
      // 跳转至成员管理页面,参数为项目在数据库中的id
      window.localStorage.setItem('id', id)
      this.$router.push({ name: 'Participants', params: { id: id }})
    },
    // 学生修改个人信息对话框弹出
    handleUpdateRole(projid) {
      this.roleInfo.projid = projid
      this.dialogUpdateForm = true
    },
    // 提交用户角色信息
    handleSubmitUpdateRole() {
      updateProRole(this.roleInfo).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '修改个人信息成功',
            type: 'success'
          })
        }
        this.$router.push('/project/list')
        this.dialogUpdateForm = false
      })
    },
    // 添加项目动态对话框
    handleAddTrends(projid) {
      this.dialogAddTrends = true
      this.proTrends.projid = projid
    },
    // 添加项目动态
    handleAddProgress() {
      const files = this.$refs.upload.uploadFiles
      if (files.length === 0) {
        addProgress(this.proTrends).then(res => {
          if (res.code === 200) {
            this.$message({
              message: '添加项目动态成功',
              type: 'success'
            })
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        this.$refs.upload.submit()
      }
      this.dialogAddTrends = false
    },
    // 添加项目动态，上传附件成功后
    successSubmit() {
      this.$message({
        message: '添加项目动态成功',
        type: 'success'
      })
      this.$router.push('/project/list')
      this.dialogAddTrends = false
    },
    ModifyComment(row) {
      this.projectInfo = Object.assign({}, row)
      this.dialogCommentVisible = true
    },
    // 修改评价
    handleModify() {
      updateCommentInfo(this.projectInfo).then(() => {
        this.$message({
          message: '修改评价成功',
          type: 'success'
        })
        this.dialogCommentVisible = false
        this.$router.go(0) // 更新信息后刷新页面
      }).catch(() => {
        this.$$message.error('修改评价失败')
      })
    },
    // 删除项目
    handleDeletePro(projid) {
      deletePro(projid).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '删除项目成功',
            type: 'success'
          })
          this.fetchData()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 跳转到项目详细信息
    toDetail(projid) {
      window.localStorage.setItem('detail_id', projid)
      window.localStorage.setItem('from', 'project')
      this.$router.push({ name: 'ProjectDetail', params: { id: projid }})
    }
  }
}
</script>

<style lang="scss" scoped>
.search-container {
  margin-left: 20px;
}
</style>
